#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

use LWP::Simple;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

my $guid = '211c76f0-ec61-11de-8a39-0800200c9a66';

#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid);
my $comicName = $comic->getName();
my $urlPrefix = 'http://www.reallifecomics.com';

my $lastStripRow = $comic->getLastStrip();
my $lastStrip = 0;

my $dateParser = new DateTime::Format::Strptime(
	pattern => '%A, %b %d, %Y',
	locale  => 'en_US'
);

my $titleCreator = new DateTime::Format::Strptime(
	pattern => '%B %d, %Y',
	locale  => 'en_US'
);

# scrape information from a page
sub scrapePage {
	my $url = shift(@_) or die "$comicName: url not passed to scrapePage";
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	my $data = {};
	$data->{url} = $url;
	my @scraped;

	# scrape 'strip' value
	my $stripRegex = qr/\<div id="date"\>(.+?)\<\/div\>/;
	@scraped = $comic->scrapeUnique($page, $stripRegex);
	my $dateString = $scraped[0];
	$dateString =~ s/\s+/ /g;
	my $date = $dateParser->parse_datetime($dateString)
	           or die "$comicName: could not parse date ($dateString)";
	my $strip = $date->epoch();

	if($strip <= $lastStrip) {
		$strip = $lastStrip; #die "$comicName: $title is out of order?";
	}
	$strip += 1000;
	$lastStrip = $strip;
	$data->{strip} = $strip;

	# generate 'title'
	$data->{title} = $titleCreator->format_datetime($date);

	# scrape 'image' value
	my $imageRegex = qr/\<img src="\/comics\/(.+?)"/;
	@scraped = $comic->scrapeUnique($page, $imageRegex);
	$data->{image} = $urlPrefix.'/comics/'.$scraped[0];

	return $data;
}


my $stripPage = get($lastStripRow->{url})
                or die "$comicName: last page url not found";
my $lastStripData = scrapePage($lastStripRow->{url}, $stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);

$lastStrip = $lastStripRow->{strip};
while($stripPage =~ /\<a href="([^"\>]+?)"\>\<img src="\/images\/nav_next\.png"/) {
	my $url = $urlPrefix.$1;
	$stripPage = get($url) or die "$comicName: next strip page not found";

	#print "$url\n";
	$comic->addStrip(scrapePage($url, $stripPage));
}

$comic->commit();

